package com.baidu.sggc.core.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Configuration
public class ThreadPoolConfig {

    @Bean(name = "mailExecutor")
    public ExecutorService mailExecutor() {
        return new ThreadPoolExecutor(
                2,                      // 核心线程数
                4,                      // 最大线程数
                60L,                    // 空闲线程存活时间
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100), // 有界队列
                new ThreadFactoryBuilder().setNameFormat("mail-sender-%d").build(), // 线程命名
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
        );
    }
}
